﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Summary description for Class1
/// </summary>
public class SanphamDAO
{
    public static bool KtratontaiSP(SanphamDTO spDTO)
    {
        try
        {
            string sQuery = "select * from SanPham where MaSP = '" + spDTO.MaSP1 + "' and TenSp = '" + spDTO.TenSP1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemSP(SanphamDTO sptDTO)
    {
        try
        {
            string sQuery = "insert into SanPham (MaSP,TenSp,HangSX,NgayNhap,TinhTrang,ChiTiet,Gia,KichThuoc,Hinh,HinhAnh,KN,NgayGioiThieu) values('" + sptDTO.MaSP1 + "','" + sptDTO.TenSP1 + "','" + sptDTO.HanSX1 + "','" + sptDTO.NgayNhap1 + "','" + sptDTO.TinhTrang1 + "','" + sptDTO.ChiTiet1 + "','" + sptDTO.Gia1 + "','" + sptDTO.Kichthuoc1 + "','" + sptDTO.Hinh1 + "','" + sptDTO.HinhAnh1 + "','" + sptDTO.KhaNang1 + "','" + sptDTO.NgayGioiThieu1 + "')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool HaSP(SanphamDTO sptDTO)
    {
        try
        {
            string sQuery = "update SanPham Set KN = '-1' where MaSP='"+sptDTO.MaSP1+"'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }

    }
    public static bool NangSP(SanphamDTO sptDTO)
    {
        try
        {
            string sQuery = "update SanPham set KN = '1' where  MaSP='" + sptDTO.MaSP1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }

    }
    public static bool SuaSPS(SanphamDTO sptDTO)
    {
        try
        {
            string sQuery = "update SanPham set TenSP='" + sptDTO.TenSP1 + "',HangSX='" + sptDTO.HanSX1 + "',TinhTrang='" + sptDTO.TinhTrang1 + "',ChiTiet='" + sptDTO.ChiTiet1 + "',Gia='" + sptDTO.Gia1 + "',KichThuoc='" + sptDTO.Kichthuoc1 + "',Hinh='" + sptDTO.Hinh1 + "',HinhAnh='" + sptDTO.HinhAnh1 + "',KN='" + sptDTO.KhaNang1 + "',NgayGioiThieu='" + sptDTO.NgayGioiThieu1 + "' where MaSP='" + sptDTO.MaSP1+ "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemCM(SanphamDTO cmsDTO , UserDTO cmuDTO)
    {
        try
        {
            string sQuery = "insert into Commnent (NgayDang,NOIDUNG,ID,MaSP) values('" + cmsDTO.Ngaydang+ "','" + cmsDTO.NoiDungCM1+ "','" + cmuDTO.ID1 + "','"+cmsDTO.MaSP1+"')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool XoaCM(SanphamDTO cmsDTO)
    {
        try
        {
            string sQuery = " DELETE FROM Commnent WHERE MACM= "+cmsDTO.MACM1+"";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    internal static bool SuaCM(SanphamDTO cmsDTO)
    {
        throw new NotImplementedException();
    }
    public static bool KtratonLike(SanphamDTO KTLikesDTO, UserDTO KTLikeuDTO)
    {
        try
        {
            string sQuery = "select * from LikeSanPham where MaSP = '" + KTLikesDTO.MaSP1 + "' and ID = '" + KTLikeuDTO.ID1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool KtratonXem(SanphamDTO KTXemsDTO, UserDTO KTXemuDTO)
    {
        try
        {
            string sQuery = "select * from Xem where MaSP = '" + KTXemsDTO.MaSP1 + "' and ID = '" + KTXemuDTO.ID1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemLike(SanphamDTO LikesDTO, UserDTO LikeuDTO)
    {
        try
        {
            string SQuery = "insert into LikeSanPham (MaSP,ID,ThoiGian) values('" + LikesDTO.MaSP1 + "','" + LikeuDTO.ID1 + "','" + LikesDTO.Ngaydang + "')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;
            
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemLikeSP(SanphamDTO LikesDTO)
    {
        try
        {
            string SQuery = "update SanPham set LikeSanPham='" + LikesDTO.TangLike1 + "' where MaSP='" + LikesDTO.MaSP1+ "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemXem(SanphamDTO XemsDTO, UserDTO XemuDTO)
    {
        try
        {
            string SQuery = "insert into Xem (MaSP,ID,ThoiGian) values('" + XemsDTO.MaSP1 + "','" + XemuDTO.ID1 + "','" + XemsDTO.Ngaydang + "')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemXemSp(SanphamDTO XemsDTO)
    {
        try
        {
            string SQuery = "update SanPham set XemSanPham='" + XemsDTO.TangXem1 + "' where MaSP='" + XemsDTO.MaSP1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemGioHang(SanphamDTO GioHangsDTO, UserDTO GioHanguDTO)
    {
        try
        {
            string SQuery = "insert into GioHang (NgayTao,ID,DiaChiGiao,SODTGIAO,KiemTra,TongThanhTien) values('" + GioHangsDTO.Ngaydang + "','" + GioHanguDTO.ID1 + "','" + GioHanguDTO.DiaChi1 + "','" + GioHanguDTO.SODT1 + "','" + GioHanguDTO.Check1 + "','" + GioHangsDTO.Gia1 + "')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool ThemChiTietGioHang(SanphamDTO GioHangsDTO)
    {
        try
        {
            string SQuery = "insert into ChiTietGioHang (MaGioHang,MaSP,SoLuongDat) values('" + GioHangsDTO.MaGioHang1 + "','" + GioHangsDTO.MaSP1 + "','" + GioHangsDTO.Soluong1 + "')";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static string LayMaGioHang()
    {
        string sQuery = "select top 1 MaGioHang from GioHang order by MaGioHang desc";
        OleDbConnection con = DataProvider.KetNoiDuLieu();
        DataTable dt = DataProvider.Laydulieu(con,sQuery);
        DataProvider.Dongketnoi(con);
        string kq = dt.Rows[0][0].ToString();
        return kq;
    }
    public static bool KtraGioHang(SanphamDTO KTgioHangDTO ,UserDTO KTgioHangSDTO)
    {
        try
        {
            string sQuery = "select * from GioHang where MaGioHang = " + KTgioHangDTO.MaGioHang1 + " and ( KiemTra = '" + KTgioHangSDTO.Check1 + "' Or  KiemTra = '" + KTgioHangSDTO.Check21 + "') and ID='" + KTgioHangSDTO.ID1 + "'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            DataTable dt = DataProvider.Laydulieu(con, sQuery);
            DataProvider.Dongketnoi(con);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool CapNhapKiemTraGioHang(SanphamDTO CNKTGHDTO,UserDTO CNKTGHSDTO)
    {
        try
        {
            string SQuery = "update GioHang set KiemTra='"+CNKTGHSDTO.Check1+"' where MaGioHang=" +  CNKTGHDTO.MaGioHang1 + " and ID='"+CNKTGHSDTO.ID1+"'";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool XacNhanKiemTraGioHang(SanphamDTO XNNKTGHDTO, UserDTO XNKTGHSDTO)
    {
        try
        {
            string SQuery = "update GioHang set KiemTra='" + XNKTGHSDTO.Check1 + "', NgayDuyet='"+XNNKTGHDTO.Ngaydang+"' where MaGioHang=" + XNNKTGHDTO.MaGioHang1 + "";
            OleDbConnection con = DataProvider.KetNoiDuLieu();
            bool KQ = DataProvider.executeNonQuery(SQuery, con);
            DataProvider.Dongketnoi(con);
            return KQ;

        }
        catch (Exception ex)
        {
            return false;
        }
    }
}